﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Manager : MonoBehaviour {

    private static Manager _instance;
    public static Manager GetInstance() {
        if (_instance == null)
        {
            _instance = new Manager();
        }
        return _instance;
    }

    public Dictionary<Player, int> currentPlayerList = new Dictionary<Player, int>();

    //每个玩家的最大血量
    public int MaxHp = 10;
    //当前血量
    public int CurrentHP = 0;

    //玩家自己的控制类
    public Player SelfController;


    public GameObject Portals;
    public GameObject Replay;
    GameObject currentPortal;
    // Use this for initialization
    void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
    public void ReStartGame()
    {
        Debug.LogError(" ReStartGame");
    }
    //新玩家加入，更新当前玩家列表
    public void NewPlay(Player pc, bool isServer)
    {
        //存储用户信息
        currentPlayerList.Add(pc, MaxHp);
        if (isServer)
        {
            SelfController = pc;
        }
        //当有一个用户加入时开始游戏
        if (currentPlayerList.Count == 1)
        {
            ReStartGame();
        }
        CurrentHP += MaxHp;
    }


    //收到攻击
    // Update is called once per frame
    public void UnderAttack()
    {
        CurrentHP--;
        if (CurrentHP < 0)
        {
            EndGame();
        }
    }

    //游戏结束逻辑，删除传送门（怪物）然后显示重玩
    void EndGame()
    {
        //删除传送门
        Destroy(currentPortal);
        //非服务器不显示重玩
        if (SelfController.isServer)
        {
            Replay.SetActive(true);
        }
        else
        {
            Replay.SetActive(false);
        }
    }

    //获取自己当前的transform
    public Transform GetTargetTransfrom()
    {
        return SelfController.transform;
    }

}
